home *** CD-ROM | disk | FTP | other *** search
- /***
- *alloc.c - malloc/free front-end
- *
- *Copyright (c) 1993-1994, Gregg Jennings. All wrongs reserved.
- * P O Box 200, Falmouth, MA 02541-0200
- *
- *Purpose:
- * Debugging support for DISKED.C.
- *
- *Notice:
- * This progam may be freely used and distributed. Any distrubution
- * with modifications must retain the above copyright statement and
- * modifications noted.
- * No pulp-publication, in whole or in part, permitted without
- * permission (magazines or books).
- *******************************************************************************/
-
- /*
- Version 2.1 13-Jan-1994 Borland stuff, heaptest was heapcheck
- Version 2.0 28-Nov-1993
-
- Usage:
-
- these can be called explicitly (as DISKED does) or via macros:
-
- #define malloc(s) alloc(1,s)
- #define free(p) freep(p)
- etc.
-
- which will enable them to be used if needed for debugging
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
-
- #include "alloc.h"
- #include "error.h" /* for global error messages */
-
-
- /*********** COMMON DATA ***********/
-
- /* for use by debug.c -- statistical stuff only */
-
- unsigned int blocks = 0;
- unsigned int nblocks = 0;
- unsigned int frees = 0;
- unsigned int nfrees = 0;
-
- void *alloc(size_t num, size_t size)
- {
- if (size == 0) /* NOTE: MSC WILL allocate */
- { /* a zero length block -- a pointer */
- error.num = ALLOC_ZERO; /* to nothing as paradoxical as */
- return NULL; /* that sounds (is?) */
- }
- ++blocks;
- return calloc(num,size);
- }
-
- #ifndef __BORLANDC__
-
- void _near *nalloc(size_t num, size_t size)
- {
- if (size == 0)
- {
- error.num = ALLOC_ZERO;
- return NULL;
- }
- ++nblocks;
- return _ncalloc(num,size);
- }
-
- #endif
-
- /* realloc() */
-
- void *newalloc(void *p, size_t size)
- {
- if (p)
- freep(p);
- return alloc(1,size);
- }
-
- void freep(void *addr)
- {
- if (addr != NULL)
- {
- free(addr);
- if (_heapset(254) != _HEAPOK)
- error.num = HEAP_ERROR;
- addr = NULL;
- ++frees;
- }
- else
- error.num = FREE_NULL;
- }
-
- #ifndef __BORLANDC__
-
- void nfreep(void _near *addr)
- {
- if (addr != NULL)
- {
- _nfree(addr);
- if (_nheapset(254) != _HEAPOK)
- error.num = HEAP_ERROR;
- addr = NULL;
- ++nfrees;
- }
- else
- error.num = FREE_NULL;
- }
-
- #endif
-
- /* heapcheck()
-
- putting calls to this throughout the program can help find were the
- heap is getting corrupted:
-
- if (heaptest() < 1)
- printf("%s%d",__FILE__,__LINE__);
- */
-
- int heaptest(void)
- {
- int h;
-
- if ((h=_heapchk()) != _HEAPOK || (h=_nheapchk()) != _HEAPOK)
- return h;
- else
- return 1;
- }
-
- /* AS IN MALLOC.H:
-
- constants for _heapchk/_heapset/_heapwalk routines
-
- #define _HEAPEMPTY (-1)
- #define _HEAPOK (-2) // _heapchk/_heapset only
- #define _HEAPBADBEGIN (-3)
- #define _HEAPBADNODE (-4)
- #define _HEAPEND (-5) // _heapwalk only
- #define _HEAPBADPTR (-6)
- #define _FREEENTRY 0
- #define _USEDENTRY 1
-
- */
- static char *heap_msg[] = {
- "",
- "empty heap",
- "heap is fine",
- "bad start of heap",
- "bad node in heap",
- "end of heap",
- "bad pointer to heap",
- "bad heap",
- };
-
- /* get heap status message */
-
- char *heapstat(int status)
- {
- status = -status; /* make offset into message array */
-
- if (status < 1 || status > sizeof(heap_msg)/sizeof(char *))
- status = sizeof(heap_msg)/sizeof(char *);
- return heap_msg[status];
- }
-